package components.contorls.superClass
{
    import components.core.UIComponent;
    import components.fte.FTEElement;
    import components.utils.TimeoutIntervalUtil;
    
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.filters.GlowFilter;
    import flash.geom.Rectangle;
    import flash.text.engine.TextBaseline;
    import flash.text.engine.TextBlock;
    import flash.text.engine.TextLine;
    import flash.utils.clearInterval;
    import flash.utils.clearTimeout;

    /**
     *一个一个字的显示文本，并且可以自动翻页
     * @author zn
     *
     */
    [Event(name = "complete", type = "flash.events.Event")]

    public class BaseShowTextTween extends UIComponent
    {
        private var _text:String = "";

        private var _showIntervalID:uint;

        private var _turnPageTimeoutID:uint;

        private var _tfMask:Shape = new Shape();

        private var _chatIndex:uint = 0;

        /**
         *显示一个字符的间隔时间
         */
        private var _chatIntervalTime:uint = 40;

        /**
         *显示下一页的间隔时间
         */
        private var _turnPageWaitTime:uint = 5000;

        private var _playing:Boolean = false;

        private var _isShowCurrentPageAll:Boolean = false;

        private var _textChange:Boolean;

		/**
		 *文本显示Sprite 
		 */		
        protected var _textSp:Sprite;

		/**
		 *文本块 
		 */		
        protected var _textBlock:TextBlock;

		/**
		 *textLine列表 
		 */		
        private var _textLineList:Array;

		/**
		 *当前显示的textLine Index
		 */		
        private var _pageTextLineIndex:uint=0;

		/**
		 *行间距 
		 */		
		private var _vGap:Number=4;

		/**
		 *页中的所有textLine 
		 */		
		private var _pageTextLineList:Array=[];

		private var _fteElement:FTEElement;
		
		private var _fontName:String="宋体";
		
		private var _fontSize:Number=12;
		
		private var _fontColor:int=0x000000;
		
		private var _bold:Boolean=false;
		
        public function BaseShowTextTween()
        {
            super();
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();

            if (!_initialized)
            {
				if(numChildren>0)
					removeChildAt(0);
				
                _textSp = new Sprite();
                addChild(_textSp);

                _tfMask = new Shape();
                _tfMask.x = _textSp.x;
                _tfMask.y = _textSp.y;

                addChild(_tfMask);
                _textSp.mask = _tfMask;

                _textBlock = new TextBlock();
                _textBlock.baselineZero = TextBaseline.IDEOGRAPHIC_TOP;
				
				_fteElement=new FTEElement();
				
				setTFFilter();
            }

            if (_textChange)
            {
                clearInterval(_showIntervalID);
                createElement();
            }
        }

        protected override function measureSizes():Boolean
        {
            return super.measureSizes();
        }


        protected override function updateDisplayList():void
        {
            super.updateDisplayList();

            if (_textChange)
            {
                startTween();
                _textChange = false;
            }
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/


        [Inspectable(defaultValue = "")]
        public function get text():String
        {
            return _text;
        }

        public function set text(value:String):void
        {
            _text = value;
            _textChange = true;
            validateNow();
        }

        [Inspectable(defaultValue = "40")]
        public function get chatIntervalTime():uint
        {
            return _chatIntervalTime;
        }

        public function set chatIntervalTime(value:uint):void
        {
            _chatIntervalTime = value;

            if (playing)
            {
                clearInterval(_showIntervalID);
                _showIntervalID = TimeoutIntervalUtil.addSetInterval(this,chatInterval, chatIntervalTime);
            }
        }

        [Inspectable(defaultValue = "5000")]
        public function get turnPageWaitTime():uint
        {
            return _turnPageWaitTime;
        }

        public function set turnPageWaitTime(value:uint):void
        {
            _turnPageWaitTime = value;
        }

		
		[Inspectable(defaultValue = "4",type="Number")]
		public function get vGap():Number
		{
			return _vGap;
		}
		
		public function set vGap(value:Number):void
		{
			_vGap = value;
		}

        /***************************************
         *
         * 其他代码
         *
         * **************************************/
        protected override function setTFFilter():void
        {
            if (tfFilterEnable)
                _textSp.filters = [ new GlowFilter(tfFilterColor, 1, 2, 2, 200)];
            else
                _textSp.filters = [];
        }

        /**
         *执行一个一个显示字符的动画
         *
         */
        private function startTween():void
        {
            _playing = true;
			cleanTextSp();
            showTextLineTween();
        }

        /**
         *显示一行
         *
         */
        private function showTextLineTween():void
        {
            clearInterval(_showIntervalID);
            clearTimeout(_turnPageTimeoutID);

            if (_textLineList.length == 0)
            {
				dispatchEvent(new Event(Event.COMPLETE));
				_playing = false;
                return;
            }
			
			_isShowCurrentPageAll = false;
			cleanTextSp();
			
			_pageTextLineIndex=0;
			_pageTextLineList=[];
			var textLineBottom:Number=0;
			var textLine:TextLine;
			
			while(true)
			{
				textLine = _textLineList.shift();
				
				if(textLine==null)
					break;
				
				textLine.y = textLineBottom;
				textLineBottom = textLine.y + textLine.height+vGap;
				
				if(textLineBottom>height)
				{
					_textLineList.unshift(textLine);
					break;
				}
				
				_textSp.addChild(textLine);
				
				_pageTextLineList.push(textLine);
			}
			
            _chatIndex = 0;

            _showIntervalID = TimeoutIntervalUtil.addSetInterval(this,chatInterval, chatIntervalTime);
        }

        private function cleanTextSp():void
        {
			_tfMask.graphics.clear();
            while (_textSp.numChildren > 0)
                _textSp.removeChildAt(0);
        }

        /**
         *显示一个字符
         *
         */
        private function chatInterval():void
        {
			var textLine:TextLine=_pageTextLineList[_pageTextLineIndex];
			
            var rect:Rectangle = textLine.getAtomBounds(_chatIndex);

            _tfMask.graphics.beginFill(0xFFFFFF, 1);
            _tfMask.graphics.drawRect(textLine.x + rect.x, textLine.y + rect.y, rect.width, rect.height);
            _tfMask.graphics.endFill();

            if (_chatIndex >= (textLine.atomCount - 1))
            {
				//显示下一行
				_chatIndex=0;
				_pageTextLineIndex++;
				
				if(_pageTextLineIndex>=_pageTextLineList.length)
					showPageComplete();
				
				return;
            }

            _chatIndex++;
        }

        /**
         *一页显示完后等待一定时间，然后显示下一页
         *
         */
        private function showPageComplete():void
        {
            _isShowCurrentPageAll = true;

            clearInterval(_showIntervalID);
            clearTimeout(_turnPageTimeoutID);
            _turnPageTimeoutID = TimeoutIntervalUtil.addSetTimeout(this,showTextLineTween, turnPageWaitTime);
        }

        /**
         *是否正在显示动画
         * @return
         *
         */
        public function get playing():Boolean
        {
            return _playing;
        }

        /**
         *显示当前页的全部
         *
         */
        public function showCurrentPageAll():void
        {
            if (_textChange)
                return;

            _tfMask.graphics.beginFill(0xFFFFFF, 1);
            _tfMask.graphics.drawRect(0, 0, width, height);
            _tfMask.graphics.endFill();
			
			showPageComplete();
        }

        /**
         *是否已显示完当前页的所有字符
         */
        public function get isShowCurrentPageAll():Boolean
        {
            return _isShowCurrentPageAll;
        }

        /**
         *显示下一页
         *
         */
        public function nextPage():void
        {
            if (_textChange)
                return;

            showTextLineTween();
        }

        protected function createElement():void
        {
            _textBlock.content = _fteElement.createElement(text);

            createTextLine();
        }

        public function createTextLine():void
        {
            var textLine:TextLine = _textBlock.createTextLine(null, width);

            _textLineList = [];

            while (textLine)
            {
                _textLineList.push(textLine);
                textLine = _textBlock.createTextLine(textLine, width);
            }
			
			_textBlock.releaseLineCreationData();
        }

		[Inspectable(defaultValue="宋体")]
		public function get fontName():String
		{
			return _fontName;
		}

		public function set fontName(value:String):void
		{
			_fontName = value;
			_fteElement.defaultFontName=value;
		}

		[Inspectable(defaultValue="12")]
		public function get fontSize():Number
		{
			return _fontSize;
		}

		public function set fontSize(value:Number):void
		{
			_fontSize = value;
			_fteElement.defaultFontSize=value;
		}

		[Inspectable(defaultValue="#000000", type="Color")]
		public function get fontColor():int
		{
			return _fontColor;
		}

		public function set fontColor(value:int):void
		{
			_fontColor = value;
			_fteElement.defaultFontColor=value;
		}

		[Inspectable(defaultValue="false")]
		public function get bold():Boolean
		{
			return _bold;
		}

		public function set bold(value:Boolean):void
		{
			_bold = value;
			_fteElement.defaultBold=value;
		}
    }
}
